package com.yc.demo.d0729;

import com.google.gson.Gson;
import com.yc.demo.util.Utils;
import com.yc.demo.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    // doPost => post 请求
    // doGet  => get 请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String username = req.getParameter("username");
        final String password = req.getParameter("password");
        final String vcode1 = req.getParameter("vcode");
        final String vcode2 = (String) req.getSession().getAttribute("vcode");
        Result result;
        if (vcode1 == null || vcode1.isEmpty()) {
            result = new Result(0, "请输入验证码!", username);
        } else if( vcode1.equalsIgnoreCase(vcode2) == false){
            result = new Result(0, "验证码错误!", username);
        } else if (("yc".equals(username) || "zs".equals(username) || "ls".equals(username) || "ww".equals(username))
                && "123".equals(password)) {
            // 使用会话保存登录的状态
            final HttpSession session = req.getSession();
            Map<String,Object> data = new HashMap<>();
            data.put("username",username);
            data.put("requestURI", session.getAttribute("requestURI"));
            // 清空保存的地址
            session.setAttribute("requestURI",null);
            result = new Result(1, "登录成功", data);
            // 在会话中保存登录的用户名, 会话中的数据会在交互过程中一直有效
            session.setAttribute("loginedUser", username);
            // 请求对象中保存的数据, 只在当前请求中有效
            //req.setAttribute("loginedUser", username);
            // 清除会话中的验证码
            session.removeAttribute("vcode");
        } else {
            result = new Result(0, "登录失败", null);
        }
        Utils.result(resp, result);
    }
}
